<?php
/*
  $Id$

  Copyright (C) 2007 - 2010 iclear GmbH

                    All rights reserved.

  This program is free software licensed under the GNU General Public License (GPL).

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA

*************************************************************************/
  if(!class_exists('IclearWrapperMage', false)) {
    require dirname(__FILE__) . '/magento.base.php';
  }

  define('IC_WRAPPER_ID', 'magento-1.x' );

  class IclearWrapper extends IclearWrapperMage  {
    
// class constructor
    function __construct(&$icCore) {
      $this->icVersion = '$Id$';
      global $icCore;
      parent::__construct($icCore);
    }
    
    function storeOrder() {
      $rc = false;
      if($basket = $this->icCore->getBasket()) {
        global $SESS_LIFE;

        // $SESS_LIFE used in session modules / functions 2 determine session lifetime (time() + $SESS_LIFE)
        $SESS_LIFE = 1440;
        $icLang = $this->icCore->getLanguage();
        
		session_id($basket->sessionID());
        $session = Mage::getSingleton('checkout/session');

        $customer_id = $basket->customerID();
        $order = $basket->getOrder();
        
        switch($basket->language()) {
          case 'EN':
            $language = 'english';
            break;
            
          case 'DE':
          default:
            $language = 'german';
            break;
        }

        $onepage = Mage::getSingleton('checkout/type_onepage');
        $onepage->getQuote()->collectTotals()->save();
        $onepage->saveOrder();
        // set correct order state if it's an waiting order
        $orderID = $onepage->getCheckout()->getLastOrderId();
        $basket->orderID($orderID);
        $order = Mage::getSingleton('sales/order')->load($orderID);
        $payment = Mage::getSingleton('Iclear_Sendorderb2c_Model_Iclearpayment');
        
        
        if($basket->status() == IC_ORDER_WAIT) {

        	$order->setStatus($payment->getPendingState());
      		$order->addStatusToHistory(
						        $payment->getPendingState(),
						        'iclear payment in pending state!');
        	$order->save();
        }
        
        $cart = Mage::getSingleton('checkout/cart');
        $cart->init();
        $cart->save();
        

      }
      return $rc;
    }
  }

?>
